<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="list_q" ref="list_q_ref" inline>
        <el-form-item
          v-for="(
            { form_item_prop: { label, prop }, component_name, component_prop },
            index
          ) in list_q_c"
          :key="index"
          v-bind="{ label, prop }"
        >
          <component
            :is="component_name"
            v-model="list_q[prop]"
            v-bind="component_prop"
            :placeholder="label"
          >
            <template v-if="component_name == 'el-select'">
              <el-option
                v-for="i in component_prop.option_data"
                :key="i.id"
                :value="i[component_prop.option_prop.value]"
                :label="i[component_prop.option_prop.label]"
              ></el-option>
            </template>
          </component>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="get_list">搜索</el-button>
          <el-button @click="reset_list_q">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="open_add_dialog" plain>新增</el-button>
      <el-button
        :disabled="del_rows.length == 0"
        type="danger"
        plain
        @click="del_row(true)"
        >删除</el-button
      >
      <el-table :data="list" @selection-change="(val) => (del_rows = val)">
        <el-table-column type="selection"></el-table-column>
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column width="200px" label="操作" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="open_edit_dialog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="del_row(false, row)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="
          (val) => {
            pager.limit = val;
            get_list();
          }
        "
        @current-change="
          (val) => {
            pager.page = val;
            get_list();
          }
        "
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
        style="text-align: right; margin-top: 20px"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      @close="reset_list_e"
      center
      width="1500px"
    >
      <el-form :model="list_e" ref="list_e_ref" inline>
        <el-form-item
          v-for="(
            {
              form_item_prop: { label, prop, required },
              component_name,
              component_prop,
            },
            index
          ) in list_e_c"
          :key="index"
          v-bind="{ label, prop, required }"
        >
          <component
            :is="component_name"
            v-model="list_e[prop]"
            v-bind="component_prop"
            :placeholder="label"
          >
            <template v-if="label == '领料编号'">
              <el-button slot="append" @click="open_issue_dialog"
                >选择</el-button
              >
            </template>
          </component>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog :visible.sync="visible_issue" title="选择领料单" center>
      <el-table :data="list_issue">
        <el-table-column
          v-for="item in list_issue_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column width="100px" label="操作" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="select_issue(row)"
            >选择</el-button
          >
        </el-table-column>
      </el-table>
      <el-divider></el-divider>
      <el-table :data="list_material">
        <el-table-column
          v-for="item in list_material_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column width="200px" label="操作" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="select_material(row)"
            >选择</el-button
          >
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import config_mixin from './config_mixin'
import { getIncomingInspectionList, getissueList, editIncomingInspection, getissueDetailList, addIncomingInspection, delIncomingInspection } from '@/api_zc/mes/quality-management/incoming-inspection'
export default {
  mixins: [config_mixin],
  mounted () {
    this.get_list()
    this.get_unity_list()
  },
  data () {
    return {
      list: [],
      visible: false,
      title: '',
      is_add: false,
      visible_issue: false,
      customerList: [],
      id: 0,
      del_rows: [],
      list_issue: [],
      list_material: [],
    }
  },
  methods: {
    async get_unity_list () {
      const data1 = await getissueList()
      this.list_issue = data1.data.content
    },
    async get_list () {
      if (!this.list_q.inspectionAttr) {
        return
      }
      [this.list_q.inspectionStartDate, this.list_q.inspectionEndDate] = this.list_q.inspectionAttr
      const { data: { total, content } } = await getIncomingInspectionList({ ...this.list_q, ... this.pager })
      this.list = content
      this.pager.total = total
    },
    reset_list_q () {
      this.$refs.list_q_ref.resetFields()
      this.get_list()
    },
    reset_list_e () {
      this.$refs.list_e_ref.resetFields()
      this.list_material = []
    },
    open_edit_dialog (row) {
      console.log(row)
      this.visible = true
      this.$nextTick(() => {
        Object.assign(this.list_e, {
          ...row,
          issueNo: row.issue.issueNo,
          materialName: row.material.name,
          issueId: row.issue.id,
          materialId: row.material.id
        })
      })
      this.title = '修改产品检验单'
      this.is_add = false
    },
    open_add_dialog () {
      this.visible = true
      this.title = '添加产品检验单'
      this.is_add = true
    },
    del_row (flag, row) {
      let text, ids
      if (flag) {
        text = this.del_rows.map(item => item.id).join(',')
        ids = this.del_rows.map(item => item.id)
      } else {
        text = row.id
        ids = [row.id]
      }
      this.$confirm(`是否确认删除销售合同编号为${text}的数据项？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delIncomingInspection({ ids })
        this.$message.success('删除成功')
        this.get_list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    open_issue_dialog () {
      this.visible_issue = true
    },
    async select_issue (row) {
      this.list_e.issueId = row.id
      this.list_e.issueNo = row.issueNo
      const data = await getissueDetailList({ id: row.id })
      this.list_material = data.data
      console.log(this.list_material)
    },
    select_material (row) {
      this.list_e.materialName = row.material.name
      this.list_e.materialId = row.material.id
      this.visible_issue = false
    },
    async submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善表单')
        }
        if (this.is_add) {
          await addIncomingInspection({ ...this.list_e })
          this.$message.success('添加成功')
        } else {
          await editIncomingInspection({ ...this.list_e })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.get_list()
      })
    },
  }
}
</script>